package com.imooc.bilibili.api;

import com.imooc.bilibili.domain.core.JsonResponse;
import com.imooc.bilibili.domain.following.FollowingGroup;
import com.imooc.bilibili.domain.following.UserFollowing;
import com.imooc.bilibili.following.service.UserFollowingService;
import com.imooc.bilibili.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户关注Api
 *
 * @author gcq
 * @Create 2022-05-24
 */
@RestController
public class UserFollowingApi {

    @Autowired
    UserFollowingService userFollowingService;

    @Autowired
    private UserSupport userSupport;


    /**
     * 关注用户
     *
     * @param userFollowing 用户关注
     * @return 结果
     */
    @PostMapping("user-followings")
    public JsonResponse<String> addUserFollowing(@RequestBody UserFollowing userFollowing) {
        Long userId = userSupport.getCurrentUserId();
        userFollowing.setUserId(userId);
        userFollowingService.addUserFollowings(userFollowing);
        return JsonResponse.success();
    }

    /**
     * 获取用户关注列表
     *
     * @return 结果
     */
    @GetMapping("/user-following")
    public JsonResponse<List<FollowingGroup>> getUserFollowing() {
        Long userId = userSupport.getCurrentUserId();
        List<FollowingGroup> userFollowings = userFollowingService.getUserFollowings(userId);
        return new JsonResponse<>(userFollowings);
    }

    /**
     * 获取用户粉丝列表
     *
     * @return 结果
     */
    @GetMapping("/user-fans")
    public JsonResponse<List<UserFollowing>> getUserFans() {
        Long userId = userSupport.getCurrentUserId();
        List<UserFollowing> userFans = userFollowingService.getUserFans(userId);
        return new JsonResponse<>(userFans);
    }


    /**
     * 添加用户分组信息
     *
     * @param followingGroup 用户分组
     * @return 结果
     */
    @PostMapping("user-following-groups")
    public JsonResponse<Long> addUserFollowingGroups(@RequestBody FollowingGroup followingGroup) {
        Long userId = userSupport.getCurrentUserId();
        followingGroup.setUserId(userId);
        Long groupId = userFollowingService.addUserFollowingGroups(followingGroup);
        return new JsonResponse<>(groupId);
    }

    /**
     * 查询用户分组信息
     *
     * @return 结果
     */
    @GetMapping("/user-following-groups")
    public JsonResponse<List<FollowingGroup>> getUserFollowingGroup() {
        Long userId = userSupport.getCurrentUserId();
        List<FollowingGroup> followingGroupList = userFollowingService.getUserFollowingGroups(userId);
        return new JsonResponse<>(followingGroupList);
    }
}